# 作用域
x = 1
scope = vars()  # vars的内置函数，它返回这个不可见的字典：成为命名空间，或作用域
print(scope['x'])  # 1


# globals() 全局作用域
def visit_global_x(x):
    print(f"local x={x},global x={globals()['x']}")


visit_global_x(123)  # local x=123,global x=1


# 修改全局变量
def modify_global_x():
    global x
    x = x + 1


modify_global_x()
print(x)  # 2


# nonlocal
def outer():
    base = 100

    def inner():
        nonlocal base
        base += 100

    print(f'before outer base={base}')
    inner()
    print(f'after outer base={base}')


outer()
# before outer base=100
# after outer base=200
